import { GenerationCommand, SimpleGenerationCommand } from '../generator';
import { Token } from '../parser';
import { getFirstDefinedOrDefault } from '../utils/nullable';

export class Code extends GenerationCommand {
	generate: (token: Token) => string = (token => {
		return `<code>${token.content}</code>`;
	});
	name: string = 'code';
}

export class Heading extends SimpleGenerationCommand {
	name: string = 'h1';
	wrap: (str: string) => string = (str => `<h1>${str}</h1>`);
}

export class Bold extends SimpleGenerationCommand {
	name: string = 'b';
	wrap: (str: string) => string = (str => `<strong>${str}</strong>`);
}

export class Color extends SimpleGenerationCommand {
	name: string = 'color';
	wrap: (str: string, param?: Map<string, string>) => string = ((str, param) => {
		if (param) {
			return `<span class="${getFirstDefinedOrDefault(param, ['0', 'color', 'colour'], 'black')}">${str}</span>`;
		} else {
			return str;
		}
	});
}

export class Image extends SimpleGenerationCommand {
	name: string = 'img';
	wrap: (str: string, param?: Map<string, string>) => string = ((str, param) => {
		if (param) {
			return `<img src="${getFirstDefinedOrDefault(param, ['0', 'src', 'href'], '404.png')}" alt="${getFirstDefinedOrDefault(param, ['1', 'alt', 'caption'], 'No caption')}">`;
		} else {
			// this use is deprecated
			return `<img src="${str}" alt="No caption">`;
		}
	});
}
